package com.clannad.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clannad.commonutils.CourseWebVoOrder;
import com.clannad.commonutils.UcenterMember;
import com.clannad.eduorder.entity.Order;
import com.clannad.eduorder.feign.EduClient;
import com.clannad.eduorder.feign.UcenterClient;
import com.clannad.eduorder.mapper.OrderMapper;
import com.clannad.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clannad.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author clannad
 * @since 2022-08-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public String createOrders(String courseId, String memberId) {
        Order order=new Order();
        UcenterMember ucenterMember  = ucenterClient.getInfo(memberId);
        CourseWebVoOrder courseDto = eduClient.getCourseInfoORder(courseId);
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseDto.getTitle());
        order.setCourseCover(courseDto.getCover());
        order.setTeacherName(courseDto.getTeacherName());
        order.setTotalFee(courseDto.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMember.getMobile());
        order.setNickname(ucenterMember.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        this.save(order);
        return order.getOrderNo();
    }

    @Override
    public void deleteStatusByZero() {
        LambdaQueryWrapper<Order> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getStatus,0);
        this.remove(lambdaQueryWrapper);
    }
}
